<template>
  <div class="equipment-manage">
    <el-row :gutter="20">
      <div v-for="(item, index) in cardItems"
           :key="index">
        <el-col v-if="item.show"
                class="col-bottom"
                :span="6">
          <CardItem :name="item.name"
                    :index="index"
                    :fulfill="item.fulfill"
                    :src="require('@/static/images/' + item.image)">
            <component :is="item.component"
                       :value="item.value"
                       @goBigDataApply="goBigDataApply" />
          </CardItem>
        </el-col>
      </div>
    </el-row>
  </div>
</template>

<script>
import { seal, tenant } from "@/api/index";
import CardItem from './components/CardItem.vue'
import AppreciateSeal from './components/AppreciateSeal.vue'
import AppreciateReceipt from './components/AppreciateReceipt.vue'
import AppreciateExpand from './components/AppreciateExpand.vue'
import AppreciateAnalyse from './components/AppreciateAnalyse.vue'
import cert from '@/api/modules/cert';

function findMenuItem (menuMap, menuName, ...childNames) {
  let item = menuMap.get(menuName);
  if (!item) {
    return null;
  }
  for (let childName of childNames) {
    let childItem = item.children?.find(({ name }) => name === childName);
    if (!childItem) {
      return null;
    }
    item = childItem;
  }
  return item.menuId;
}

const SERVICE_NAME = '增值服务';
const ANALYSIS_NAME = '大数据分析';
const APPLY_NAME = '大数据申请';
const DIGITAL_RECEIPT = '数字化回单';

export default {
  name: "Appreciation",
  components: { CardItem, AppreciateSeal, AppreciateReceipt, AppreciateExpand, AppreciateAnalyse },
  data () {
    return {
      sealDetail: {},
      config: {}, // 租户配置
      certCount: {}, // 数字化回单单数
      menuArr: sessionStorage.getItem("ANYWHERE::MENU_TREE"),
      bigDataShow: {},
      bigDataAnalysis: {},
    };
  },
  computed: {
    cardItems () {
      return [
        { name: '电子印章', image: 'seal.png', component: 'AppreciateSeal', value: this.sealDetail, show: true, fulfill: this.goDetail },
        { name: '数字化回单', image: 'digitalReceipt.png', component: 'AppreciateReceipt', value: this.certCount, show: this.config.certSwitch, fulfill: this.goDigitalReceipt },
        { name: '大数据展示', image: 'dataView.png', component: 'AppreciateExpand', value: this.bigDataShow, show: this.bigDataShow.configSwitch },
        { name: '大数据分析', image: 'dataAnalys.png', component: 'AppreciateAnalyse', value: this.bigDataAnalysis, show: this.bigDataAnalysis.configSwitch },
      ]
    },
  },
  created () {
    this.getApplyByTenantId();
    this.getTenantInfo();
    this.getTenantBigData();
  },
  methods: {
    // 获取当前租户信息
    async getTenantInfo () {
      let { data } = await tenant.getTenantInfo();
      const { config } = data;
      this.config = config;
      // certSwitch 数字化回单
      if (config.certSwitch) {
        this.getCertPushNum();
      }
    },
    // 查询数字化回单单数
    async getCertPushNum () {
      let response = await cert.getCertCount();
      this.certCount = response.data
      // this.certCount = response.data || 0
    },
    // 获取当前租户大数据配置
    async getTenantBigData () {
      let { data } = await tenant.getTenantBigData();
      const { bigDataAnalysis, bigDataShow } = data;
      this.bigDataAnalysis = bigDataAnalysis;
      this.bigDataShow = bigDataShow;
    },
    // 查询印章信息
    async getApplyByTenantId () {
      let response = await seal.getApplyByTenantId();
      const { data } = response
      this.sealDetail = data
    },
    goDetail () {
      this.$router.push({
        path: "/openApplication",
        query: {
          applyId: this.sealDetail?.applyId,
          applyType: this.sealDetail?.auditStatus === 2 ? 2 : 1
        }
      });
    },
    goDigitalReceipt () {
      try {
        let menuList = JSON.parse(this.menuArr);
        if (!Array.isArray(menuList)) {
          throw new Error('无效的menuArr格式');
        }
        let menuMap = new Map();
        for (let item of menuList) {
          menuMap.set(item.name, item);
        }
        let menuId = findMenuItem(menuMap, SERVICE_NAME, DIGITAL_RECEIPT);
        if (!menuId) {
          throw new Error('未找到menuId');
        }
        this.$router.push({
          path: `/board/${menuId}`,
        });
      } catch (error) {
        console.error(error);
      }
    },
    goBigDataApply () {
      try {
        let menuList = JSON.parse(this.menuArr);
        if (!Array.isArray(menuList)) {
          throw new Error('无效的menuArr格式');
        }
        let menuMap = new Map();
        for (let item of menuList) {
          menuMap.set(item.name, item);
        }
        let menuId = findMenuItem(menuMap, SERVICE_NAME, ANALYSIS_NAME, APPLY_NAME);
        if (!menuId) {
          throw new Error('未找到menuId');
        }
        this.$router.push({
          path: `/board/${menuId}`,
        });
      } catch (error) {
        console.error(error);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.equipment-manage {
  height: calc(100vh - 138px);
  background-color: #fff;
  padding: 20px;
}
.col-bottom {
  padding-bottom: 20px;
}
</style>
